using UnityEngine;
using System.Net;
using System.Collections;
using System.Collections.Generic;

public delegate void OnMouseClicSpellIdentifierDelegate(Spells Spell);

public class TimeLine : MonoBehaviour {

	public List <SpellIdentifier> listSpellIdentifierObjects;
	public Hud hud;
	public List <Spells> listOfSelectedSpells;
	
	// Use this for initialization
	void Awake () {
		listOfSelectedSpells = new List<Spells>();
		UpdateSpellIdentifier();
	}
	
	public void ClearTimeline()
	{
		listOfSelectedSpells = new List<Spells>();
		UpdateSpellIdentifier();
	}
	
	public int countTurns()
	{
		int count=0;
		foreach (Spells spell in listOfSelectedSpells)
		{
			count+=spell.NbTour;
		}
		return count;
	}
	
	//desactive colliders
	public void DesactivateColliders()
	{
		foreach (SpellIdentifier spellIdentifier in listSpellIdentifierObjects)
		{
			spellIdentifier.transform.collider.enabled = false;
		}
	}
	
	// Add listofSelectedSpell
	public void AddSpells (Spells spell) {
		if ( countTurns()+spell.NbTour<=10)
		{
			Debug.Log("ADD");
			listOfSelectedSpells.Add(spell);
			UpdateSpellIdentifier();
		}
	}
	
	// Remove listofSelectedSpell
	public void RemoveSpells (Spells spell) {
			Debug.Log("Remove");
			listOfSelectedSpells.Remove(spell);
			UpdateSpellIdentifier();
	}
	
	// Construit l'intérieur de la timeline du début à la fin
	void UpdateSpellIdentifier () 
	{
		int index = 0;
		Debug.Log("COUNT " + listOfSelectedSpells.Count + " " + listSpellIdentifierObjects.Count);
 		foreach (Spells spell in listOfSelectedSpells)
		{
			Texture2D image = spell.Image;
			int nbTours = spell.NbTour;
			bool first = true;
			for ( int i=index; i< nbTours+index; i++)
			{
				Debug.Log("index " + index + " i " + i + " spell " + spell + " identifier " + listSpellIdentifierObjects[i]);
				listSpellIdentifierObjects[i].transform.renderer.enabled = true;
				listSpellIdentifierObjects[i].transform.renderer.material.mainTexture = image;
				//si premier active et redimentionne le colider
				if (first)
				{
					listSpellIdentifierObjects[i].transform.collider.enabled = true;
					listSpellIdentifierObjects[i].InitSpellIdentifier(spell, RemoveSpells);
					BoxCollider boxCollider = listSpellIdentifierObjects[i].transform.collider.GetComponent<BoxCollider>();
					switch ( nbTours )
					{
						case 1:
							boxCollider.center = Vector3.zero;
							boxCollider.size = new Vector3(10f, 1f, 10f);
							break;
						case 2:
							boxCollider.center = new Vector3(-5f,0f, 0f);
							boxCollider.size = new Vector3(20f, 1f, 10f);
							break;
						case 3:
							boxCollider.center = new Vector3(-10f,0f, 0f);
							boxCollider.size = new Vector3(30f, 1f, 10f);
							break;
						case 4:
							boxCollider.center = new Vector3(-15f,0f, 0f);;
							boxCollider.size = new Vector3(40f, 1f, 10f);
							break;
						default:
							Debug.LogError("ERROR, NBTURN NOT BETWEEN 1 AND 4");
							break;
					}	
					first = false;
				}
				else
				{
					listSpellIdentifierObjects[i].transform.collider.enabled = false;
				}
			}
			index=index+nbTours;
			hud.UpdateIconAvailable();
		}
		//tout ce qui n'a pas été selectionné
		for(int j = index; j<listSpellIdentifierObjects.Count; j++)
		{
			listSpellIdentifierObjects[j].transform.renderer.enabled = false;
			listSpellIdentifierObjects[j].transform.collider.enabled = false;
		}
	}
}
